{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bb04f1a2-1f87-42e6-a5ad-453eec40215f",
   "metadata": {},
   "source": [
    "# Models API\n",
    "\n",
    "使用 Models API 查看和访问 OpenAI 提供的预训练大语言模型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7466990e-c2ac-441c-a545-fdc122e752ca",
   "metadata": {},
   "source": [
    "## List Models\n",
    "\n",
    "列出当前可用的模型，并提供每个模型的基本信息，如所有者和可用性。\n",
    "\n",
    "`data`: 目前OpenAI提供的大语言模型列表，列表中的每一项都对应着一个模型实例。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f453ab0c-b262-4fa5-993e-b4b341e46a94",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import openai\n",
    "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "models = openai.Model.list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "87dbc132-41cd-4ebc-928f-9edf6a4befcd",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"object\": \"list\",\n",
      "  \"data\": [\n",
      "    {\n",
      "      \"id\": \"davinci\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649359874,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-uJaD4C9nXA6tPNoBII9hcYF4\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692634268,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"davinci\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"gpt-4\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1687882411,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-tsET7VSHRhiQHP5Qz6TU1FBb\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1693330062,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": false,\n",
      "          \"allow_logprobs\": false,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": false,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"gpt-4\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"gpt-3.5-turbo-16k-0613\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1685474247,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-njFlPWRSXLDM2LGcW12W8M1d\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1693820681,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"gpt-3.5-turbo-16k-0613\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-davinci-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649364042,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-CDlahk1RbkghXDjtxqzXoPNo\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690913868,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-davinci-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-search-curie-query-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172509,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-fNgpMH6ZEQulSq1CjzlfQuIe\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864192,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-search-curie-query-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"gpt-3.5-turbo\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1677610602,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-aRL68Vu37kh2Z0wi2tmPhGZT\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692458060,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"gpt-3.5-turbo\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"babbage\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649358449,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-h574xGeqWyBeFDDKaoVTC4CO\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692394129,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"babbage\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-babbage-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649364043,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-YABzYWjC1kS6M2BnI6Fr9vuS\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690913878,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-babbage-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"gpt-4-0613\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1686588896,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-XgbDsLBjXy3aVd6xZvWJFmW2\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1693537171,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": false,\n",
      "          \"allow_logprobs\": false,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": false,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"gpt-4-0613\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"gpt-3.5-turbo-16k\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1683758102,\n",
      "      \"owned_by\": \"openai-internal\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-05YdajzL8nXihXaK1lxklgPs\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1693820702,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"gpt-3.5-turbo-16k\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"curie-instruct-beta\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649364042,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-4GYfzAdSMcJmQvF7bsw01UWw\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690863785,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"curie-instruct-beta\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"davinci-similarity\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172509,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-XHJ9P2cvfDAl6Q6NABs6wD7G\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864520,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"davinci-similarity\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"code-davinci-edit-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649880484,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-T8Ie7SvlPyvtsDvPlfC8DftZ\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690915089,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"code-davinci-edit-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-similarity-curie-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172507,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-ZQZGhVQCQSN4WC1wRJsFZfRL\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864230,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-similarity-curie-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"ada-code-search-text\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172510,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-jWFKGhnNYXhMIJuYYBe8zKoH\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864242,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"ada-code-search-text\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"gpt-3.5-turbo-0613\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1686587434,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-t8jT1BkYxMNFFB57ndHNpyfP\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692640208,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"gpt-3.5-turbo-0613\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-search-ada-query-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172505,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-YO36k119sJYqPB8yHh737z8l\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864529,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-search-ada-query-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"babbage-search-query\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172509,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-o5hcKERXLlTSB0nfq8fPkAzK\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864257,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"babbage-search-query\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"ada-similarity\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172507,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-Tz8CgePTpeDdl0q0mDxAseS4\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864543,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"ada-similarity\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-curie-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649364043,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-epkpKCMZ4ezExtQH69gwuIVE\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692389420,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-curie-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-search-ada-doc-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172507,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-WQoo7GOoaleCrrerQ8ROIejy\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864068,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-search-ada-doc-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-search-babbage-query-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172509,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-PHk5XyJIMEk88v14M2eEcJfE\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864079,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-search-babbage-query-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"code-search-ada-code-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172507,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-lPJ8tQWzTuRpZpOjtRN4CjlP\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864269,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"code-search-ada-code-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"curie-search-document\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172508,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-o3nt5yDhE7FpA8PtMlzGuW3k\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864552,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"curie-search-document\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"davinci-002\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1692634301,\n",
      "      \"owned_by\": \"system\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-i5MgZCdMtQ4dZeuGaY9cZ6zG\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692720923,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"davinci-002\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-search-davinci-query-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172505,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-X2U9yi1RKudh1hGQ9CnPth2A\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864090,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-search-davinci-query-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-search-curie-doc-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172509,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-7mOkCIwOIehlltLDPM1oSKN7\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864279,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-search-curie-doc-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"babbage-search-document\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172510,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-FQiAIZXWHZ4yJl6b4X0JWpfw\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864561,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"babbage-search-document\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"babbage-002\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1692634615,\n",
      "      \"owned_by\": \"system\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-tC8uL3ohDEVSzUvcgZCv33gV\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692720928,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"babbage-002\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"babbage-code-search-text\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172509,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-9AyTgRlbDLetEnvXKDgJvSvR\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864101,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"babbage-code-search-text\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-embedding-ada-002\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1671217299,\n",
      "      \"owned_by\": \"openai-internal\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-6xIVIGILGxg9gTVWY0SpzkFn\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692313508,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-embedding-ada-002\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"davinci-instruct-beta\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649364042,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-ZNpXjNy0lDniBWzpvi6w6wSU\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690842588,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"davinci-instruct-beta\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"davinci-search-query\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172505,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-w5yjX7u1Hgz0jJFhPRB93n6I\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864112,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"davinci-search-query\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-similarity-babbage-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172505,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-8p0vOyyD6xVDYv6XOC4EYIin\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864583,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-similarity-babbage-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-davinci-002\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649880484,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-Ao62Dd2uu76ec6Koq1ksR2rj\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864376,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-davinci-002\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"code-search-babbage-text-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172507,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-uH251hsudZq0DqxtTcSYFTcD\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864593,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"code-search-babbage-text-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-davinci-003\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1669599635,\n",
      "      \"owned_by\": \"openai-internal\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-8tPGPaSHf5yGydNlBlyWKtFw\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692916017,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-davinci-003\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-search-davinci-doc-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172505,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-sqcSr7AYu6WYtzWgysHg1zO4\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864126,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-search-davinci-doc-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"code-search-ada-text-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172507,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-1JbI0GFKw9luPgTJQut1uJNe\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864601,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"code-search-ada-text-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"gpt-4-0314\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1687882410,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-AK02E3SrXhuSjwJAWvcSvR3q\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1693536116,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": false,\n",
      "          \"allow_logprobs\": false,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": false,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"gpt-4-0314\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"ada-search-query\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172505,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-cBtmsjrTZIJUKgjS8G6uALKM\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864138,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"ada-search-query\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-similarity-ada-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172505,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-fSDlSniO72T5MvD6ieDRue0a\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864457,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-similarity-ada-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"ada-code-search-code\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172505,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-469coJJMBDffmGlbftht9QR7\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864147,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"ada-code-search-code\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"whisper-1\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1677532384,\n",
      "      \"owned_by\": \"openai-internal\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-Qbzvr0DnIyt2HdaFiLj0P5h4\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692314508,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"whisper-1\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-davinci-edit-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649809179,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-bwEWUtGiBcdX0p1D1ayafH8w\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690915020,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-davinci-edit-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"davinci-search-document\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172509,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-1jEFSTL1yLUnTyI8TekKPGQF\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864158,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"davinci-search-document\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"curie-search-query\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172509,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-fvYLh7mrZBoEXRa9teCq7ZsK\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864488,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"curie-search-query\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"babbage-similarity\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172505,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-XBmFjRKu34Qvm9Y8Vjg6si3V\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864610,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"babbage-similarity\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"ada\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649357491,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-gUTBN2NFYvvG3UwNeDnY6EEe\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692393802,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"ada\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"ada-search-document\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172507,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-jEtYYVTVutQ4BLh2DnGd9tJt\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864171,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"ada-search-document\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-ada-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649364042,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-jRuB7xBCdj159SqaDmpPgeWO\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690915029,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-ada-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-similarity-davinci-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172505,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-CoAjJ7mSHeO28X7KowOnwvj9\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864500,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-similarity-davinci-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"curie-similarity\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172510,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-gSmuEPu9Q8KjQhJ5myLNKIIV\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864620,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"curie-similarity\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"babbage-code-search-code\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172509,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-UdNutuGVhzb5EBzlkaztBdMH\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864182,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"babbage-code-search-code\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"code-search-babbage-code-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172507,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-0mO5qmzzKUVVVZ9MIHTnwjwK\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864510,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"code-search-babbage-code-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"text-search-babbage-doc-001\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1651172509,\n",
      "      \"owned_by\": \"openai-dev\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-dvJNsLdOcnLbIYlRZRnfQAfX\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1690864628,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": true,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"text-search-babbage-doc-001\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"gpt-3.5-turbo-0301\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1677649963,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-I4IcSJFYZl2fIK0DPSBkgK3d\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1691712139,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"gpt-3.5-turbo-0301\",\n",
      "      \"parent\": null\n",
      "    },\n",
      "    {\n",
      "      \"id\": \"curie\",\n",
      "      \"object\": \"model\",\n",
      "      \"created\": 1649359874,\n",
      "      \"owned_by\": \"openai\",\n",
      "      \"permission\": [\n",
      "        {\n",
      "          \"id\": \"modelperm-cXKYjZ0dQvxFHt2rqkdTWYCz\",\n",
      "          \"object\": \"model_permission\",\n",
      "          \"created\": 1692392433,\n",
      "          \"allow_create_engine\": false,\n",
      "          \"allow_sampling\": true,\n",
      "          \"allow_logprobs\": true,\n",
      "          \"allow_search_indices\": false,\n",
      "          \"allow_view\": true,\n",
      "          \"allow_fine_tuning\": false,\n",
      "          \"organization\": \"*\",\n",
      "          \"group\": null,\n",
      "          \"is_blocking\": false\n",
      "        }\n",
      "      ],\n",
      "      \"root\": \"curie\",\n",
      "      \"parent\": null\n",
      "    }\n",
      "  ]\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(models)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88fae1bd-fe3d-4308-9e76-2b9bdac35f85",
   "metadata": {},
   "source": [
    "#### Prompt：获取所有模型名称 \n",
    "\n",
    "```\n",
    "下面的 json 数据存在 models 变量中，我希望遍历获取 models 中每一个 \"id\"，并将结果存在 model_list 中，生成python代码：\n",
    "\n",
    "\"data\": [\n",
    "    {\n",
    "      \"created\": 1649358449,\n",
    "      \"id\": \"babbage\",\n",
    "      \"object\": \"model\",\n",
    "      \"owned_by\": \"openai\",\n",
    "      \"parent\": null,\n",
    "      \"permission\": [\n",
    "        {\n",
    "          \"allow_create_engine\": false,\n",
    "          \"allow_fine_tuning\": false,\n",
    "          \"allow_logprobs\": true,\n",
    "          \"allow_sampling\": true,\n",
    "          \"allow_search_indices\": false,\n",
    "          \"allow_view\": true,\n",
    "          \"created\": 1669085501,\n",
    "          \"group\": null,\n",
    "          \"id\": \"modelperm-49FUp5v084tBB49tC4z8LPH5\",\n",
    "          \"is_blocking\": false,\n",
    "          \"object\": \"model_permission\",\n",
    "          \"organization\": \"*\"\n",
    "        }\n",
    "      ],\n",
    "      \"root\": \"babbage\"\n",
    "    },\n",
    "    {\n",
    "      \"created\": 1669599635,\n",
    "      \"id\": \"text-davinci-003\",\n",
    "      \"object\": \"model\",\n",
    "      \"owned_by\": \"openai-internal\",\n",
    "      \"parent\": null,\n",
    "      \"permission\": [\n",
    "        {\n",
    "          \"allow_create_engine\": false,\n",
    "          \"allow_fine_tuning\": false,\n",
    "          \"allow_logprobs\": true,\n",
    "          \"allow_sampling\": true,\n",
    "          \"allow_search_indices\": false,\n",
    "          \"allow_view\": true,\n",
    "          \"created\": 1688551385,\n",
    "          \"group\": null,\n",
    "          \"id\": \"modelperm-jepinXYt59ncUQrjQEIUEDyC\",\n",
    "          \"is_blocking\": false,\n",
    "          \"object\": \"model_permission\",\n",
    "          \"organization\": \"*\"\n",
    "        }\n",
    "      ],\n",
    "      \"root\": \"text-davinci-003\"\n",
    "    },\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "72513bee-c571-453d-80ba-76bbb5f206b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_list = [model['id'] for model in models['data']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c0eae95d-8ef2-4f16-a8fd-dfa5dcb53149",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['davinci', 'gpt-4', 'gpt-3.5-turbo-16k-0613', 'text-davinci-001', 'text-search-curie-query-001', 'gpt-3.5-turbo', 'babbage', 'text-babbage-001', 'gpt-4-0613', 'gpt-3.5-turbo-16k', 'curie-instruct-beta', 'davinci-similarity', 'code-davinci-edit-001', 'text-similarity-curie-001', 'ada-code-search-text', 'gpt-3.5-turbo-0613', 'text-search-ada-query-001', 'babbage-search-query', 'ada-similarity', 'text-curie-001', 'text-search-ada-doc-001', 'text-search-babbage-query-001', 'code-search-ada-code-001', 'curie-search-document', 'davinci-002', 'text-search-davinci-query-001', 'text-search-curie-doc-001', 'babbage-search-document', 'babbage-002', 'babbage-code-search-text', 'text-embedding-ada-002', 'davinci-instruct-beta', 'davinci-search-query', 'text-similarity-babbage-001', 'text-davinci-002', 'code-search-babbage-text-001', 'text-davinci-003', 'text-search-davinci-doc-001', 'code-search-ada-text-001', 'gpt-4-0314', 'ada-search-query', 'text-similarity-ada-001', 'ada-code-search-code', 'whisper-1', 'text-davinci-edit-001', 'davinci-search-document', 'curie-search-query', 'babbage-similarity', 'ada', 'ada-search-document', 'text-ada-001', 'text-similarity-davinci-001', 'curie-similarity', 'babbage-code-search-code', 'code-search-babbage-code-001', 'text-search-babbage-doc-001', 'gpt-3.5-turbo-0301', 'curie']\n"
     ]
    }
   ],
   "source": [
    "print(model_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "82922dd9-f38e-4ec1-8be0-361aefb99d7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "58"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(model_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75d2a270-949c-4ad2-a568-e254e6231333",
   "metadata": {},
   "source": [
    "## Retrieve Model\n",
    "\n",
    "获取模型实例，提供有关模型的基本信息，例如所有者和权限设置。\n",
    "\n",
    "以`text-davinci-003`模型为例，解释说明各项参数：\n",
    "\n",
    "1. `created`: 这是模型创建的时间戳，单位为 Unix 时间戳（自1970年1月1日（00:00:00 GMT）以后的秒数）。\n",
    "2. `id`: 这是模型的唯一标识符。在这个例子中，模型的 ID 是 \"text-davinci-003\"。\n",
    "3. `object`: 这个字段表示的是当前对象的类型，在这个例子中，对象是 \"model\"，说明这个 JSON 对象是一个模型。\n",
    "4. `owned_by`: 这个字段表示的是模型的所有者，在这个例子中，模型的所有者是 \"openai-internal\"。\n",
    "5. `parent`: 这个字段表示的是模型的父模型。如果此字段为 null，那么说明这个模型没有父模型。\n",
    "6. `permission`: 这个字段是一个包含多个键-值对的对象，描述了该模型的访问和使用权限。例如：\n",
    "   - `allow_create_engine` 为 false，说明不允许创建引擎；\n",
    "   - `allow_fine_tuning` 为 false，说明不允许对模型进行微调；\n",
    "   - `allow_logprobs` 为 true，说明允许获取模型的 logprobs 输出；\n",
    "   - `allow_sampling` 为 true，说明允许进行采样；\n",
    "   - `allow_search_indices` 为 false，说明不允许搜索索引；\n",
    "   - `allow_view` 为 true，说明允许查看模型；\n",
    "   - `is_blocking` 为 false，说明此权限不是阻止操作的。\n",
    "8. `root`: 这个字段表示的是模型的根模型。在这个例子中，模型的根模型是 \"text-davinci-003\"。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6fd7eca9-ed5a-4bca-8f9c-a0bf36177d31",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import openai\n",
    "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "gpt_3 = openai.Model.retrieve(\"text-davinci-003\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "51078b58-67db-467a-b09f-ef40562f1fad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"id\": \"text-davinci-003\",\n",
      "  \"object\": \"model\",\n",
      "  \"created\": 1669599635,\n",
      "  \"owned_by\": \"openai-internal\",\n",
      "  \"permission\": [\n",
      "    {\n",
      "      \"id\": \"modelperm-8tPGPaSHf5yGydNlBlyWKtFw\",\n",
      "      \"object\": \"model_permission\",\n",
      "      \"created\": 1692916017,\n",
      "      \"allow_create_engine\": false,\n",
      "      \"allow_sampling\": true,\n",
      "      \"allow_logprobs\": true,\n",
      "      \"allow_search_indices\": false,\n",
      "      \"allow_view\": true,\n",
      "      \"allow_fine_tuning\": false,\n",
      "      \"organization\": \"*\",\n",
      "      \"group\": null,\n",
      "      \"is_blocking\": false\n",
      "    }\n",
      "  ],\n",
      "  \"root\": \"text-davinci-003\",\n",
      "  \"parent\": null\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(gpt_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2ea16de8-add9-4ad4-b5b9-ebb959d7ab7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Model model id=gpt-3.5-turbo at 0x7f482c2d2160> JSON: {\n",
       "  \"id\": \"gpt-3.5-turbo\",\n",
       "  \"object\": \"model\",\n",
       "  \"created\": 1677610602,\n",
       "  \"owned_by\": \"openai\",\n",
       "  \"permission\": [\n",
       "    {\n",
       "      \"id\": \"modelperm-aRL68Vu37kh2Z0wi2tmPhGZT\",\n",
       "      \"object\": \"model_permission\",\n",
       "      \"created\": 1692458060,\n",
       "      \"allow_create_engine\": false,\n",
       "      \"allow_sampling\": true,\n",
       "      \"allow_logprobs\": true,\n",
       "      \"allow_search_indices\": false,\n",
       "      \"allow_view\": true,\n",
       "      \"allow_fine_tuning\": false,\n",
       "      \"organization\": \"*\",\n",
       "      \"group\": null,\n",
       "      \"is_blocking\": false\n",
       "    }\n",
       "  ],\n",
       "  \"root\": \"gpt-3.5-turbo\",\n",
       "  \"parent\": null\n",
       "}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "openai.Model.retrieve(\"gpt-3.5-turbo\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa107114-28cd-435a-a4b0-c9ceabddf415",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "028f4f7e-3be1-44d7-bfcd-c65a13e85093",
   "metadata": {},
   "source": [
    "# 文本内容补全初探（Completions API）\n",
    "\n",
    "使用 Completions API 实现各类文本生成任务\n",
    "\n",
    "\n",
    "主要请求参数说明：\n",
    "\n",
    "\n",
    "- **`model`** （string，必填）\n",
    "\n",
    "  要使用的模型的 ID。可以参考 **模型端点兼容性表**。\n",
    "\n",
    "- **`prompt`** （string or array，必填，Defaults to ）\n",
    "\n",
    "  生成补全的提示，编码为字符串、字符串数组、token数组或token数组数组。\n",
    "\n",
    "  注意，这是模型在训练过程中看到的文档分隔符，所以如果没有指定提示符，模型将像从新文档的开头一样生成。\n",
    "\n",
    "- **`stream`** （boolean，选填，默认 false）\n",
    "\n",
    "  当它设置为 true 时，API 会以 SSE（ Server Side Event ）方式返回内容，即会不断地输出内容直到完成响应，流通过 `data: [DONE]` 消息终止。\n",
    "\n",
    "- **`max_tokens`** （integer，选填，默认是 16）\n",
    "\n",
    "  补全时要生成的最大 token 数。\n",
    "\n",
    "  提示 `max_tokens` 的 token 计数不能超过模型的上下文长度。大多数模型的上下文长度为 2048 个token（最新模型除外，它支持 4096）\n",
    "\n",
    "- **`temperature`** （number，选填，默认是1）\n",
    "\n",
    "  使用哪个采样温度，在 **0和2之间**。\n",
    "\n",
    "  较高的值，如0.8会使输出更随机，而较低的值，如0.2会使其更加集中和确定性。\n",
    "\n",
    "  通常建议修改这个（`temperature` ）或 `top_p` 但两者不能同时存在，二选一。\n",
    "\n",
    "- **`n`** （integer，选填，默认为 1）\n",
    "\n",
    "  每个 `prompt` 生成的补全次数。\n",
    "\n",
    "  注意：由于此参数会生成许多补全，因此它会快速消耗token配额。小心使用，并确保对 `max_tokens` 和 `stop` 进行合理的设置。\n",
    "\n",
    "\n",
    "## 生成英文文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0526acb1-5741-48b6-ae64-917ad0d064b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import openai\n",
    "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "data = openai.Completion.create(\n",
    "  model=\"text-davinci-003\",\n",
    "  prompt=\"Say this is a test\",\n",
    "  max_tokens=7,\n",
    "  temperature=0\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "af7a8d1f-c2f7-421d-bd0d-0890d06a0c34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"warning\": \"This model version is deprecated. Migrate before January 4, 2024 to avoid disruption of service. Learn more https://platform.openai.com/docs/deprecations\",\n",
      "  \"id\": \"cmpl-7vMd9F7mTzm28DsyWWir4e7Eyp3ON\",\n",
      "  \"object\": \"text_completion\",\n",
      "  \"created\": 1693906043,\n",
      "  \"model\": \"text-davinci-003\",\n",
      "  \"choices\": [\n",
      "    {\n",
      "      \"text\": \"\\n\\nThis is indeed a test\",\n",
      "      \"index\": 0,\n",
      "      \"logprobs\": null,\n",
      "      \"finish_reason\": \"length\"\n",
      "    }\n",
      "  ],\n",
      "  \"usage\": {\n",
      "    \"prompt_tokens\": 5,\n",
      "    \"completion_tokens\": 7,\n",
      "    \"total_tokens\": 12\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9dd45c47-fe4b-4b63-a861-6dba932e459d",
   "metadata": {},
   "source": [
    "#### Prompt：解析返回结果，获取生成文本\n",
    "\n",
    "```\n",
    "下面是 openai completion api 的返回结果，将生成的内容存放在变量 text 中:\n",
    "\n",
    "{\n",
    "  \"id\": \"cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7\",\n",
    "  \"object\": \"text_completion\",\n",
    "  \"created\": 1589478378,\n",
    "  \"model\": \"text-davinci-003\",\n",
    "  \"choices\": [\n",
    "    {\n",
    "      \"text\": \"\\n\\nThis is indeed a test\",\n",
    "      \"index\": 0,\n",
    "      \"logprobs\": null,\n",
    "      \"finish_reason\": \"length\"\n",
    "    }\n",
    "  ],\n",
    "  \"usage\": {\n",
    "    \"prompt_tokens\": 5,\n",
    "    \"completion_tokens\": 7,\n",
    "    \"total_tokens\": 12\n",
    "  }\n",
    "}\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "63cddd0f-a8f3-4f62-891a-8146306a5d95",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "# Extract the 'text' field from the first element in the 'choices' array\n",
    "text = data['choices'][0]['text']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "49cbd498-bfa5-4d11-a6ee-30822b24dcc8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "This is indeed a test\n"
     ]
    }
   ],
   "source": [
    "print(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c7fa3342-c045-4e59-bdb4-caf06853d2b4",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "InvalidRequestError",
     "evalue": "This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mInvalidRequestError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[20], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[43mopenai\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCompletion\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m      2\u001b[0m \u001b[43m  \u001b[49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mgpt-3.5-turbo\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m      3\u001b[0m \u001b[43m  \u001b[49m\u001b[43mprompt\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mSay this is a test\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m      4\u001b[0m \u001b[43m  \u001b[49m\u001b[43mmax_tokens\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m7\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m      5\u001b[0m \u001b[43m  \u001b[49m\u001b[43mtemperature\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\n\u001b[1;32m      6\u001b[0m \u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/miniconda3/envs/langchain/lib/python3.10/site-packages/openai/api_resources/completion.py:25\u001b[0m, in \u001b[0;36mCompletion.create\u001b[0;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[1;32m     23\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m     24\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 25\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     26\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m TryAgain \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m     27\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m timeout \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m time\u001b[38;5;241m.\u001b[39mtime() \u001b[38;5;241m>\u001b[39m start \u001b[38;5;241m+\u001b[39m timeout:\n",
      "File \u001b[0;32m~/miniconda3/envs/langchain/lib/python3.10/site-packages/openai/api_resources/abstract/engine_api_resource.py:153\u001b[0m, in \u001b[0;36mEngineAPIResource.create\u001b[0;34m(cls, api_key, api_base, api_type, request_id, api_version, organization, **params)\u001b[0m\n\u001b[1;32m    127\u001b[0m \u001b[38;5;129m@classmethod\u001b[39m\n\u001b[1;32m    128\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcreate\u001b[39m(\n\u001b[1;32m    129\u001b[0m     \u001b[38;5;28mcls\u001b[39m,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    136\u001b[0m     \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mparams,\n\u001b[1;32m    137\u001b[0m ):\n\u001b[1;32m    138\u001b[0m     (\n\u001b[1;32m    139\u001b[0m         deployment_id,\n\u001b[1;32m    140\u001b[0m         engine,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    150\u001b[0m         api_key, api_base, api_type, api_version, organization, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mparams\n\u001b[1;32m    151\u001b[0m     )\n\u001b[0;32m--> 153\u001b[0m     response, _, api_key \u001b[38;5;241m=\u001b[39m \u001b[43mrequestor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    154\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mpost\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m    155\u001b[0m \u001b[43m        \u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    156\u001b[0m \u001b[43m        \u001b[49m\u001b[43mparams\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mparams\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    157\u001b[0m \u001b[43m        \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    158\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    159\u001b[0m \u001b[43m        \u001b[49m\u001b[43mrequest_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    160\u001b[0m \u001b[43m        \u001b[49m\u001b[43mrequest_timeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest_timeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    161\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    163\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m stream:\n\u001b[1;32m    164\u001b[0m         \u001b[38;5;66;03m# must be an iterator\u001b[39;00m\n\u001b[1;32m    165\u001b[0m         \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(response, OpenAIResponse)\n",
      "File \u001b[0;32m~/miniconda3/envs/langchain/lib/python3.10/site-packages/openai/api_requestor.py:298\u001b[0m, in \u001b[0;36mAPIRequestor.request\u001b[0;34m(self, method, url, params, headers, files, stream, request_id, request_timeout)\u001b[0m\n\u001b[1;32m    277\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrequest\u001b[39m(\n\u001b[1;32m    278\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m    279\u001b[0m     method,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    286\u001b[0m     request_timeout: Optional[Union[\u001b[38;5;28mfloat\u001b[39m, Tuple[\u001b[38;5;28mfloat\u001b[39m, \u001b[38;5;28mfloat\u001b[39m]]] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m    287\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Tuple[Union[OpenAIResponse, Iterator[OpenAIResponse]], \u001b[38;5;28mbool\u001b[39m, \u001b[38;5;28mstr\u001b[39m]:\n\u001b[1;32m    288\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mrequest_raw(\n\u001b[1;32m    289\u001b[0m         method\u001b[38;5;241m.\u001b[39mlower(),\n\u001b[1;32m    290\u001b[0m         url,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    296\u001b[0m         request_timeout\u001b[38;5;241m=\u001b[39mrequest_timeout,\n\u001b[1;32m    297\u001b[0m     )\n\u001b[0;32m--> 298\u001b[0m     resp, got_stream \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_interpret_response\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresult\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    299\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m resp, got_stream, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mapi_key\n",
      "File \u001b[0;32m~/miniconda3/envs/langchain/lib/python3.10/site-packages/openai/api_requestor.py:700\u001b[0m, in \u001b[0;36mAPIRequestor._interpret_response\u001b[0;34m(self, result, stream)\u001b[0m\n\u001b[1;32m    692\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m (\n\u001b[1;32m    693\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_interpret_response_line(\n\u001b[1;32m    694\u001b[0m             line, result\u001b[38;5;241m.\u001b[39mstatus_code, result\u001b[38;5;241m.\u001b[39mheaders, stream\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m    695\u001b[0m         )\n\u001b[1;32m    696\u001b[0m         \u001b[38;5;28;01mfor\u001b[39;00m line \u001b[38;5;129;01min\u001b[39;00m parse_stream(result\u001b[38;5;241m.\u001b[39miter_lines())\n\u001b[1;32m    697\u001b[0m     ), \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m    698\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    699\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m (\n\u001b[0;32m--> 700\u001b[0m         \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_interpret_response_line\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    701\u001b[0m \u001b[43m            \u001b[49m\u001b[43mresult\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcontent\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mutf-8\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    702\u001b[0m \u001b[43m            \u001b[49m\u001b[43mresult\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstatus_code\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    703\u001b[0m \u001b[43m            \u001b[49m\u001b[43mresult\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    704\u001b[0m \u001b[43m            \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m    705\u001b[0m \u001b[43m        \u001b[49m\u001b[43m)\u001b[49m,\n\u001b[1;32m    706\u001b[0m         \u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m    707\u001b[0m     )\n",
      "File \u001b[0;32m~/miniconda3/envs/langchain/lib/python3.10/site-packages/openai/api_requestor.py:765\u001b[0m, in \u001b[0;36mAPIRequestor._interpret_response_line\u001b[0;34m(self, rbody, rcode, rheaders, stream)\u001b[0m\n\u001b[1;32m    763\u001b[0m stream_error \u001b[38;5;241m=\u001b[39m stream \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124merror\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m resp\u001b[38;5;241m.\u001b[39mdata\n\u001b[1;32m    764\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m stream_error \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;241m200\u001b[39m \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m rcode \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m300\u001b[39m:\n\u001b[0;32m--> 765\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhandle_error_response(\n\u001b[1;32m    766\u001b[0m         rbody, rcode, resp\u001b[38;5;241m.\u001b[39mdata, rheaders, stream_error\u001b[38;5;241m=\u001b[39mstream_error\n\u001b[1;32m    767\u001b[0m     )\n\u001b[1;32m    768\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m resp\n",
      "\u001b[0;31mInvalidRequestError\u001b[0m: This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?"
     ]
    }
   ],
   "source": [
    "data = openai.Completion.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  prompt=\"Say this is a test\",\n",
    "  max_tokens=7,\n",
    "  temperature=0\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c43d83bd-375f-4256-925a-e894d0e155a0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "159f1bf9-1604-414c-af74-45b22c33a7f9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "5c79be11-b2b4-4580-ad10-b7111cb950ea",
   "metadata": {},
   "source": [
    "## 生成中文文本\n",
    "\n",
    "调整 `max_tokens` "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3e3a4018-7c72-49a1-942b-167c3941fb96",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = openai.Completion.create(\n",
    "  model=\"text-davinci-003\",\n",
    "  prompt=\"讲10个给程序员听得笑话\",\n",
    "  max_tokens=1000,\n",
    "  temperature=0.5\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f04959ba-5781-4e9e-bdfb-a55f0698acd9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "1. 两个程序员正在玩游戏，突然有一只蚂蚁爬上来，第一个程序员用筷子把它按下，第二个程序员说：“你这是在Debug？”\n",
      "\n",
      "2. 一个程序员在写代码，突然有一只苍蝇飞过来，他拿起电脑，把它拍死了，然后说：“这是什么Bug？”\n",
      "\n",
      "3. 一个程序员看到一个报错，他拿起桌子，把它扔出窗外，然后说：“这是什么Bug？”\n",
      "\n",
      "4. 一个程序员坐在电脑前，突然有一只苍蝇飞过来，他赶紧把它赶走，然后说：“这是什么Bug？”\n",
      "\n",
      "5. 一个程序员在写代码，突然有一只蚂蚁爬上来，他把它拍死了，然后说：“这是什么Bug？”\n",
      "\n",
      "6. 一个程序员在写代码，突然有一只蚂蚁爬上来，他用鼠标把它捅死了，然后说：“这是什么Bug？”\n",
      "\n",
      "7. 一个程序员在写代码，突然有一只苍蝇飞过来，他用鼠标把它拍死了，然后说：“这是什么Bug？”\n",
      "\n",
      "8. 一个程序员正在写代码，突然有一只蚂蚁爬上来，他用筷子把它按下，然后说：“这是什么Bug？”\n",
      "\n",
      "9. 一个程序员看到一个报错，他拿起电脑，把它扔出窗外，然后说：“这是什么Bug？谁来修复它？”\n",
      "\n",
      "10. 一个程序员正在写代码，突然有一只苍蝇飞过来，他用鼠标把它拍死了，然后说：“这是什么Bug\n"
     ]
    }
   ],
   "source": [
    "text = data['choices'][0]['text']\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad54c353-64bf-44dc-ad55-1d2c4a783443",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3e5bdaff-764f-4fbf-b2c2-a8afbdaca8c6",
   "metadata": {},
   "source": [
    "## 生成 Python 代码，并执行和验证\n",
    "\n",
    "以面试中考察的典型的试题 `快速排序` 为例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3d5a6a51-57cd-4890-8978-b07efa2c28e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = openai.Completion.create(\n",
    "  model=\"text-davinci-003\",\n",
    "  prompt=\"生成可执行的快速排序 Python 代码\",\n",
    "  max_tokens=1000,\n",
    "  temperature=0\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "45e24e7d-72d7-491a-a60f-0e8031a6fc7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "def quick_sort(arr):\n",
      "    if len(arr) <= 1:\n",
      "        return arr\n",
      "    pivot = arr[len(arr) // 2]\n",
      "    left = [x for x in arr if x < pivot]\n",
      "    middle = [x for x in arr if x == pivot]\n",
      "    right = [x for x in arr if x > pivot]\n",
      "    return quick_sort(left) + middle + quick_sort(right)\n"
     ]
    }
   ],
   "source": [
    "text = data['choices'][0]['text']\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "659f4384-73bc-4a7a-bfeb-8a5f49d40079",
   "metadata": {},
   "outputs": [],
   "source": [
    "def quick_sort(arr):\n",
    "    if len(arr) <= 1:\n",
    "        return arr\n",
    "    pivot = arr[len(arr) // 2]\n",
    "    left = [x for x in arr if x < pivot]\n",
    "    middle = [x for x in arr if x == pivot]\n",
    "    right = [x for x in arr if x > pivot]\n",
    "    return quick_sort(left) + middle + quick_sort(right)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "591fb1da-27bc-4d9a-9856-bffcdbf48d11",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"warning\": \"This model version is deprecated. Migrate before January 4, 2024 to avoid disruption of service. Learn more https://platform.openai.com/docs/deprecations\",\n",
      "  \"id\": \"cmpl-7vMiDbZyyAkuSwiyXBhctzsBp18nx\",\n",
      "  \"object\": \"text_completion\",\n",
      "  \"created\": 1693906357,\n",
      "  \"model\": \"text-davinci-003\",\n",
      "  \"choices\": [\n",
      "    {\n",
      "      \"text\": \"\\n\\ndef quick_sort(arr):\\n    if len(arr) <= 1:\\n        return arr\\n    pivot = arr[len(arr) // 2]\\n    left = [x for x in arr if x < pivot]\\n    middle = [x for x in arr if x == pivot]\\n    right = [x for x in arr if x > pivot]\\n    return quick_sort(left) + middle + quick_sort(right)\",\n",
      "      \"index\": 0,\n",
      "      \"logprobs\": null,\n",
      "      \"finish_reason\": \"stop\"\n",
      "    }\n",
      "  ],\n",
      "  \"usage\": {\n",
      "    \"prompt_tokens\": 25,\n",
      "    \"completion_tokens\": 99,\n",
      "    \"total_tokens\": 124\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49c6675c-611e-4933-9fca-804200eef339",
   "metadata": {},
   "source": [
    "#### Prompt：Jupyter Notebook 中执行生成的代码\n",
    "\n",
    "Prompt：\n",
    "\n",
    "```\n",
    "我现在用 Completion API 生成了  Python 代码，并以字符串形式存放在 text 中，如下所示：\n",
    "\n",
    "text = data['choices'][0]['text']\n",
    "print(text)\n",
    "\n",
    "def quick_sort(arr):\n",
    "    if len(arr) <= 1:\n",
    "        return arr\n",
    "    pivot = arr[len(arr) // 2]\n",
    "    left = [x for x in arr if x < pivot]\n",
    "    middle = [x for x in arr if x == pivot]\n",
    "    right = [x for x in arr if x > pivot]\n",
    "    return quick_sort(left) + middle + quick_sort(right)\n",
    "\n",
    "如何在 Jupyter notebook 中执行text中存放的这段代码\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "21707523-bde6-48b9-ac9d-da693045f7c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# `exec` 函数会执行传入的字符串作为 Python 代码。\n",
    "# 在这个例子中，我们使用 `exec` 来定义了一个 `quick_sort` 函数，然后你就可以调用这个函数了。\n",
    "# 请注意，`exec` 可以执行任何 Python 代码，因此在使用它的时候一定要小心，特别是当你执行的代码来自不可信的来源时。\n",
    "exec(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "15918392-560b-4826-8800-4c7a11167579",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 2, 3, 6, 8, 10, 12]\n"
     ]
    }
   ],
   "source": [
    "# 现在你可以调用这个函数了\n",
    "print(quick_sort([12,3,6,8,10,1,2,1]))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd669a78-125f-478f-b3d9-677f137d9398",
   "metadata": {},
   "source": [
    "# 聊天机器人初探（Chat Completions API）\n",
    "\n",
    "使用 Chat Completions API 实现对话任务\n",
    "\n",
    "聊天补全(Chat Completions API)以消息列表作为输入，并返回模型生成的消息作为输出。尽管聊天格式旨在使多轮对话变得简单，但它同样适用于没有任何对话的单轮任务。\n",
    "\n",
    "主要请求参数说明：\n",
    "\n",
    "\n",
    "- **`model` （string，必填）**\n",
    "\n",
    "  要使用的模型ID。有关哪些模型适用于Chat API的详细信息\n",
    "\n",
    "- **`messages` （array，必填）**\n",
    "\n",
    "  迄今为止描述对话的消息列表\n",
    "    - **`role` （string，必填）**\n",
    "\n",
    "  发送此消息的角色。`system` 、`user` 或 `assistant` 之一（一般用 user 发送用户问题，system 发送给模型提示信息）\n",
    "\n",
    "    - **`content` （string，必填）**\n",
    "    \n",
    "      消息的内容\n",
    "    \n",
    "    - **`name` （string，选填）**\n",
    "    \n",
    "      此消息的发送者姓名。可以包含 a-z、A-Z、0-9 和下划线，最大长度为 64 个字符\n",
    "\n",
    "- **`stream` （boolean，选填，是否按流的方式发送内容）**\n",
    "\n",
    "  当它设置为 true 时，API 会以 SSE（ Server Side Event ）方式返回内容。SSE 本质上是一个长链接，会持续不断地输出内容直到完成响应。如果不是做实时聊天，默认false即可。\n",
    "\n",
    "- **`max_tokens` （integer，选填）**\n",
    "\n",
    "  在聊天补全中生成的最大 **tokens** 数。\n",
    "\n",
    "  输入token和生成的token的总长度受模型上下文长度的限制。\n",
    "\n",
    "- **`temperature` （number，选填，默认是 1）**\n",
    "\n",
    "  采样温度，在 0和 2 之间。\n",
    "\n",
    "  较高的值，如0.8会使输出更随机，而较低的值，如0.2会使其更加集中和确定性。\n",
    "\n",
    "  通常建议修改这个（`temperature` ）或者 `top_p` ，但两者不能同时存在，二选一。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d00108e4-b98e-4d55-a1e2-78845b321fec",
   "metadata": {},
   "source": [
    "## 开启聊天模式\n",
    "\n",
    "使用 `messages` 记录迄今为止对话的消息列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "172f2eb6-2d2b-4d10-9887-5e4af011c77f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "messages=[\n",
    "    {\n",
    "        \"role\": \"user\", \n",
    "        \"content\": \"Hello!\"\n",
    "    }\n",
    "]\n",
    "\n",
    "\n",
    "data = openai.ChatCompletion.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages = messages\n",
    ")\n",
    "\n",
    "# data = openai.Completion.create(\n",
    "#   model=\"text-davinci-003\",\n",
    "#   prompt=\"生成可执行的快速排序 Python 代码\",\n",
    "#   max_tokens=1000,\n",
    "#   temperature=0\n",
    "# )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "158a8a3b-0a73-4157-9025-8282527fd692",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"id\": \"chatcmpl-7vMnFP1CzPANSPYY1Z2tnaXkUIlBd\",\n",
      "  \"object\": \"chat.completion\",\n",
      "  \"created\": 1693906669,\n",
      "  \"model\": \"gpt-3.5-turbo-0613\",\n",
      "  \"choices\": [\n",
      "    {\n",
      "      \"index\": 0,\n",
      "      \"message\": {\n",
      "        \"role\": \"assistant\",\n",
      "        \"content\": \"Hi there! How can I assist you today?\"\n",
      "      },\n",
      "      \"finish_reason\": \"stop\"\n",
      "    }\n",
      "  ],\n",
      "  \"usage\": {\n",
      "    \"prompt_tokens\": 9,\n",
      "    \"completion_tokens\": 10,\n",
      "    \"total_tokens\": 19\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fd2a778-d1d7-453a-aee5-5ef56eb9e591",
   "metadata": {},
   "source": [
    "#### Prompt：解析返回结果，获取生成文本，并维护聊天记录\n",
    "\n",
    "```\n",
    "我希望维护一个保存聊天记录的 messages 变量，如下所示：\n",
    "\n",
    "messages=[\n",
    "    {\n",
    "        \"role\": \"user\", \n",
    "        \"content\": \"Hello!\"\n",
    "    },\n",
    "    {\n",
    "        \"content\": \"Hi! How can I assist you today?\",\n",
    "        \"role\": \"assistant\"\n",
    "    },\n",
    "]\n",
    "\n",
    "下面是 openai chat completion api 的返回结果 ，将生成的 message 追加到 messages 结尾:\n",
    "\n",
    "{\n",
    "  \"choices\": [\n",
    "    {\n",
    "      \"finish_reason\": \"stop\",\n",
    "      \"index\": 0,\n",
    "      \"message\": {\n",
    "        \"content\": \"Hi! How can I assist you today?\",\n",
    "        \"role\": \"assistant\"\n",
    "      }\n",
    "    }\n",
    "  ],\n",
    "  \"created\": 1690106747,\n",
    "  \"id\": \"chatcmpl-7fQGBBKSXTye4HsKcKMWKFVW63qTT\",\n",
    "  \"model\": \"gpt-3.5-turbo-0613\",\n",
    "  \"object\": \"chat.completion\",\n",
    "  \"usage\": {\n",
    "    \"completion_tokens\": 9,\n",
    "    \"prompt_tokens\": 9,\n",
    "    \"total_tokens\": 18\n",
    "  }\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d872f47e-b768-4d3e-850f-c2034e2e2263",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"role\": \"assistant\",\n",
      "  \"content\": \"Hi there! How can I assist you today?\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# 从返回的数据中获取生成的消息\n",
    "new_message = data[\"choices\"][0][\"message\"]\n",
    "\n",
    "# 打印 new_message\n",
    "print(new_message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ee8c6048-2fed-4a81-90cc-45f526630aad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'role': 'user', 'content': 'Hello!'}, <OpenAIObject at 0x7f48082af470> JSON: {\n",
      "  \"role\": \"assistant\",\n",
      "  \"content\": \"Hi there! How can I assist you today?\"\n",
      "}]\n"
     ]
    }
   ],
   "source": [
    "# 将消息追加到 messages 列表中\n",
    "messages.append(new_message)\n",
    "print(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1f88a3a6-c6ad-47a2-a187-40f4e921b54f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "openai.openai_object.OpenAIObject"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(new_message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4dc4a02b-2c19-4114-bfad-7b8364d1f8fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'assistant'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_message.role"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "843e1454-85b9-409f-8a6a-c84fadcab05a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hi there! How can I assist you today?'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "626586e5-55c0-4b1c-8dc4-175edab9b7c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<OpenAIObject at 0x7f48082af470> JSON: {\n",
       "  \"role\": \"assistant\",\n",
       "  \"content\": \"Hi there! How can I assist you today?\"\n",
       "}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "messages.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "bbf5b445-f0b2-422c-befd-2738982903d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'role': 'user', 'content': 'Hello!'}]\n"
     ]
    }
   ],
   "source": [
    "print(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a9d6f7b-09fd-4bed-bcde-2ab50d3d415c",
   "metadata": {},
   "source": [
    "#### Prompt: OpenAIObject -> Dict\n",
    "\n",
    "```\n",
    "打印 messages 列表后发现数据类型不对，messages 输出如下：\n",
    "\n",
    "print(messages)\n",
    "\n",
    "[{'role': 'user', 'content': 'Hello!'}, <OpenAIObject at 0x7f27582c13f0> JSON: {\n",
    "  \"content\": \"Hello! How can I assist you today?\",\n",
    "  \"role\": \"assistant\"\n",
    "}]\n",
    "\n",
    "将OpenAIObject 转换为一个如下数据类型格式：\n",
    "\n",
    "    {\n",
    "        \"role\": \"user\", \n",
    "        \"content\": \"Hello!\"\n",
    "    }\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "8d9052f8-88ef-4b71-8fa8-42bff7304537",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_message = data['choices'][0]['message']\n",
    "new_message_dict = {\"role\": new_message.role, \"content\": new_message.content}\n",
    "type(new_message_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "699416f3-ee26-432a-8ad5-27b151f24846",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'role': 'assistant', 'content': 'Hi there! How can I assist you today?'}\n"
     ]
    }
   ],
   "source": [
    "print(new_message_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7a11a2c6-3f2b-4c35-b987-f396f839da7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将消息追加到 messages 列表中\n",
    "messages.append(new_message_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "532671cb-6ceb-472f-98c8-c86a38c08bc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'role': 'user', 'content': 'Hello!'}, {'role': 'assistant', 'content': 'Hi there! How can I assist you today?'}]\n"
     ]
    }
   ],
   "source": [
    "print(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7a5d741-5b2e-4f1a-8a74-e3b559874079",
   "metadata": {},
   "source": [
    "#### 新一轮对话"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ad2e089b-ba80-477b-92d1-c183f11082c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_chat = {\n",
    "    \"role\": \"user\",\n",
    "    \"content\": \"1.讲一个程序员才听得懂的冷笑话；2.今天是几号？3.明天星期几？\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "780f361d-b9e1-4343-8851-089d4c0aecde",
   "metadata": {},
   "outputs": [],
   "source": [
    "messages.append(new_chat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "32b195bb-52ca-4b7f-a1f6-5b183ffbd58f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'content': 'Hello!', 'role': 'user'},\n",
      " {'content': 'Hi there! How can I assist you today?', 'role': 'assistant'},\n",
      " {'content': '1.讲一个程序员才听得懂的冷笑话；2.今天是几号？3.明天星期几？', 'role': 'user'}]\n"
     ]
    }
   ],
   "source": [
    "from pprint import pprint\n",
    "\n",
    "pprint(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "54b0532a-0099-40e3-8a3d-9dc72d6eccbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = openai.ChatCompletion.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages=messages\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "edad9b8d-2b46-4cd4-ba03-51c96cc4c175",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"role\": \"assistant\",\n",
      "  \"content\": \"1. \\u4e3a\\u4ec0\\u4e48\\u8ba1\\u7b97\\u673a\\u5de5\\u7a0b\\u5e08\\u559c\\u6b22\\u4f7f\\u7528\\u9ed1\\u8272\\u952e\\u76d8\\uff1f\\n\\u56e0\\u4e3a\\u9ed1\\u8272\\u7684\\u952e\\u76d8\\u66f4\\u5feb\\uff0c\\u53ef\\u4ee5\\u51cf\\u5c11\\u8f93\\u5165\\u7684\\u65f6\\u95f4\\uff01\\n\\n2. \\u60a8\\u597d\\uff01\\u4eca\\u5929\\u662f\\u6211\\u88ab\\u521b\\u5efa\\u7684\\u65e5\\u5b50\\uff0c\\u6240\\u4ee5\\u4eca\\u5929\\u662f\\u65e0\\u6240\\u8c13\\u65e5\\u671f\\u7684AI\\u8bed\\u8a00\\u52a9\\u624b\\u3002\\n\\n3. \\u660e\\u5929\\u7684\\u661f\\u671f\\u51e0\\u53d6\\u51b3\\u4e8e\\u4eca\\u5929\\u7684\\u65e5\\u671f\\u3002\\u7531\\u4e8e\\u6211\\u65e0\\u6cd5\\u5b9e\\u65f6\\u83b7\\u53d6\\u65e5\\u671f\\u4fe1\\u606f\\uff0c\\u6211\\u65e0\\u6cd5\\u56de\\u7b54\\u8fd9\\u4e2a\\u95ee\\u9898\\u3002\\u5efa\\u8bae\\u60a8\\u67e5\\u770b\\u60a8\\u7684\\u65e5\\u5386\\u6216\\u8005\\u4f7f\\u7528\\u624b\\u673a\\u7684\\u65e5\\u671f\\u529f\\u80fd\\u6765\\u83b7\\u5f97\\u51c6\\u786e\\u7684\\u7b54\\u6848\\u3002\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "new_message = data['choices'][0]['message']\n",
    "# 打印 new_messages \n",
    "print(new_message)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62cb1494-45b9-4365-80de-247cb977c397",
   "metadata": {},
   "source": [
    "#### Prompt: 解析中文结果\n",
    "\n",
    "```\n",
    "新一轮对话的返回结果没有解析成中文，内容如下：\n",
    "\n",
    "# 打印 new_message\n",
    "print(new_message)\n",
    "\n",
    "{\n",
    "  \"content\": \"1. \\u51b7\\u7b11\\u8bdd\\uff1a\\u4e3a\\u4ec0\\u4e48\\u7a0b\\u5e8f\\u5458\\u603b\\u662f\\u559c\\u6b22\\u5750\\u5728\\u7535\\u8111\\u65c1\\u8fb9\\uff1f\\u56e0\\u4e3a\\u4ed6\\u4eec\\u89c9\\u5f97\\u90a3\\u662f\\u4ed6\\u4eec\\u7684\\u7535\\u8111\\u6905\\uff01\\uff08\\u7a0b\\u5e8f\\u5458\\u5e38\\u5e38\\u5c06\\u81ea\\u5df1\\u7684\\u6905\\u5b50\\u89c6\\u4f5c\\u91cd\\u8981\\u5de5\\u5177\\uff0c\\u56e0\\u6b64\\u4f1a\\u4ea7\\u751f\\u8fd9\\u79cd\\u7b11\\u8bdd\\uff09\\n\\n2. \\u4eca\\u5929\\u65e5\\u671f\\uff1a\\u5f88\\u62b1\\u6b49\\uff0c\\u4f5c\\u4e3a\\u4e00\\u4e2aAI\\u52a9\\u624b\\uff0c\\u6211\\u65e0\\u6cd5\\u83b7\\u53d6\\u5b9e\\u65f6\\u65e5\\u671f\\u3002\\u8bf7\\u60a8\\u67e5\\u770b\\u60a8\\u7684\\u7535\\u8111\\u3001\\u624b\\u673a\\u6216\\u5176\\u4ed6\\u8bbe\\u5907\\u4e0a\\u7684\\u65e5\\u671f\\u3002\\n\\n3. \\u660e\\u5929\\u661f\\u671f\\u51e0\\uff1a\\u540c\\u6837\\u62b1\\u6b49\\uff0c\\u6211\\u65e0\\u6cd5\\u63d0\\u4f9b\\u5177\\u4f53\\u7684\\u660e\\u5929\\u661f\\u671f\\u51e0\\u3002\\u60a8\\u53ef\\u4ee5\\u67e5\\u770b\\u60a8\\u7684\\u65e5\\u5386\\u6216\\u4f7f\\u7528\\u7535\\u5b50\\u8bbe\\u5907\\u4e0a\\u7684\\u65e5\\u671f\\u529f\\u80fd\\u6765\\u83b7\\u53d6\\u660e\\u5929\\u7684\\u661f\\u671f\\u51e0\\u3002\",\n",
    "  \"role\": \"assistant\"\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "bedc91c2-4216-4686-b66c-df012a9778c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. 为什么计算机工程师喜欢使用黑色键盘？\n",
      "因为黑色的键盘更快，可以减少输入的时间！\n",
      "\n",
      "2. 您好！今天是我被创建的日子，所以今天是无所谓日期的AI语言助手。\n",
      "\n",
      "3. 明天的星期几取决于今天的日期。由于我无法实时获取日期信息，我无法回答这个问题。建议您查看您的日历或者使用手机的日期功能来获得准确的答案。\n"
     ]
    }
   ],
   "source": [
    "# GPT-4: 从你提供的信息中看，这段文本是经过Unicode编码的中文。\n",
    "# 你可以使用Python的print()函数将其打印出来，以获取原始的中文字符串：\n",
    "print(new_message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "877f2ff6-8bac-43a9-a827-e472c5698ed4",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "903ec93e-fba9-403e-bfe4-b749f91abb0c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "7f417580-7810-4f0e-ad5c-6d2e0f9e7f42",
   "metadata": {},
   "source": [
    "## 使用多种身份聊天对话\n",
    "\n",
    "目前`role`参数支持3类身份： `system`, `user` `assistant`:\n",
    "\n",
    "\n",
    "![](images/chat_completion_api.png)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "0981fe4b-410d-4d70-b07c-78a88e46b7af",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构造聊天记录\n",
    "messages=[\n",
    "    {\"role\": \"system\", \"content\": \"你是一个乐于助人的体育界专家。\"},\n",
    "    {\"role\": \"user\", \"content\": \"2008年奥运会是在哪里举行的？\"},\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "7d135caf-b778-4d50-b738-a429398c6eaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "data = openai.ChatCompletion.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages=messages\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "e97b0145-af2f-47bb-865f-9b2a8f4e6353",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2008年夏季奥运会是在中国的北京举行的。有什么其他的问题我可以帮到你吗？\n"
     ]
    }
   ],
   "source": [
    "message = data[\"choices\"][0][\"message\"][\"content\"]\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "fd56c185-b6e0-4ade-8236-9e2549182ab2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加 GPT 返回结果到聊天记录\n",
    "messages.append({\"role\": \"assistant\", \"content\": message})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "a860210e-be0d-468e-8845-8ffceaecb034",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'role': 'system', 'content': '你是一个乐于助人的体育界专家。'},\n",
       " {'role': 'user', 'content': '2008年奥运会是在哪里举行的？'},\n",
       " {'role': 'assistant', 'content': '2008年夏季奥运会是在中国的北京举行的。有什么其他的问题我可以帮到你吗？'}]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "99bae857-2854-4090-8a4d-8e1930f70464",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第二轮对话\n",
    "messages.append({\"role\": \"user\", \"content\": \"1.金牌最多的是哪个国家？2.奖牌最多的是哪个国家？\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "f07bbb71-fe19-4582-b1f3-3ee994fcec0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'role': 'system', 'content': '你是一个乐于助人的体育界专家。'},\n",
       " {'role': 'user', 'content': '2008年奥运会是在哪里举行的？'},\n",
       " {'role': 'assistant', 'content': '2008年夏季奥运会是在中国的北京举行的。有什么其他的问题我可以帮到你吗？'},\n",
       " {'role': 'user', 'content': '1.金牌最多的是哪个国家？2.奖牌最多的是哪个国家？'}]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "a138da7f-b5b3-40a1-b30b-4e6b0c8e249f",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = openai.ChatCompletion.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages=messages\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "f531bc4d-3eeb-49f7-b717-0986f2b4af2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. 2008年奥运会金牌最多的国家是中国，共获得51枚金牌。\n",
      "2. 2008年奥运会奖牌最多的国家是美国，共获得110枚奖牌（包括36枚金牌、38枚银牌和36枚铜牌）。有其他的问题吗？\n"
     ]
    }
   ],
   "source": [
    "message = data[\"choices\"][0][\"message\"][\"content\"]\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "3500811e-3a80-4ed0-9770-a1f761dfab6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = openai.ChatCompletion.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages=[{'role': 'user', 'content': '1.金牌最多的是哪个国家？2.奖牌最多的是哪个国家？'}]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "cd59b92d-8362-4215-8865-82fe5cafce56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1. 根据2021年东京奥运会的数据，金牌最多的国家是中国，总共获得了38枚金牌。\\n\\n2. 根据2021年东京奥运会的数据，奖牌最多的国家是美国，总共获得了113枚奖牌（包括39枚金牌、41枚银牌和33枚铜牌）。'"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[\"choices\"][0][\"message\"][\"content\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25220ddb-b894-41b0-a36f-aadfc18d1b23",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
